package com.gitee.faster.redis.kernel;

import com.gitee.faster.redis.RedisServer;
import com.gitee.faster.redis.protocol.RedisProcessor;
import com.gitee.faster.redis.protocol.RedisProtocol;
import com.gitee.faster.redis.protocol.RedisRequest;
import com.gitee.faster.redis.storage.RedisDataStorage;
import lombok.Getter;
import lombok.SneakyThrows;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioQuickServer;

/**
 * @author hongda.li
 */
public class FasterRedisServer implements RedisServer {

    private final Protocol<RedisRequest> protocol = new RedisProtocol();

    private final MessageProcessor<RedisRequest> processor = new RedisProcessor();

    @Getter
    private int port;

    private AioQuickServer server;

    @SneakyThrows
    @Override
    public void start(int port) {
        this.port = port;
        this.server = new AioQuickServer(this.port, this.protocol, this.processor);
        this.server.start();

        this.initConfig();
    }

    private void initConfig() {
        RedisDataStorage storage = RedisDataStorage.getInstance();
        storage.store("client:databases", 16);
    }

    @Override
    public void shutdown() {
        this.server.shutdown();
    }
}
